package com.lktx.auto.cors;

import cn.hserver.core.ioc.annotation.Bean;
import cn.hserver.plugin.web.context.Webkit;
import cn.hserver.plugin.web.interfaces.FilterAdapter;
import io.netty.handler.codec.http.HttpMethod;

@Bean
public class CorsFilter implements FilterAdapter {
    @Override
    public void doFilter(Webkit webkit) throws Exception {
        webkit.httpResponse.setHeader("Access-Control-Allow-Origin", "*");
        webkit.httpResponse.setHeader("Access-Control-Allow-Methods", "*");
        webkit.httpResponse.setHeader("Access-Control-Allow-Credentials", "*");
        webkit.httpResponse.setHeader("Access-Control-Allow-Headers", "*");
        if (webkit.httpRequest.getRequestType().equals(HttpMethod.OPTIONS)) {
            webkit.httpResponse.sendHtml("");
        }
    }
}